package com.eshin.atuoconfig;

import java.beans.PropertyVetoException;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

//@Configuration 
//启注解事务管理  
@EnableTransactionManagement 
@MapperScan(basePackages="com.eshin.mapper",
sqlSessionFactoryRef = "sqlSessionFactoryBean")
//@ConfigurationProperties(prefix="spring.datasource") 
public class DatasourceConfig {
	@Resource(name="datasourceProperties")
	private DatasourceProperties datasourceProperties;
	//DataSource配置
	@Primary 
	@Bean(name="dataSource")
    public ComboPooledDataSource dataSource() throws PropertyVetoException {
    	System.out.println("dataSource");
    	ComboPooledDataSource dataSource = new ComboPooledDataSource();
    	dataSource.setJdbcUrl(datasourceProperties.getUrl());
    	dataSource.setUser(datasourceProperties.getUsername());
    	dataSource.setPassword(datasourceProperties.getPassword());
    	dataSource.setDriverClass(datasourceProperties.getDriver());
        return dataSource;
    }
    
    //DataSource配置
//    @Bean
//    @ConfigurationProperties(prefix="spring.datasource")
//    public DataSource dataSource() {
//        return new org.apache.tomcat.jdbc.pool.DataSource();
//    }
  //提供SqlSeesion
    @Bean(name="sqlSessionFactoryBean")
//    @Primary 
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dataSource")ComboPooledDataSource dataSource) throws Exception {
 
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        
        sqlSessionFactoryBean.setDataSource(dataSource);
 
//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 
//        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:com/eshin/mapper/*.xml"));
//        System.out.println("sqlSessionFactoryBean:"+sqlSessionFactoryBean.getObject());
        return sqlSessionFactoryBean.getObject();
    }
    
    @Bean
    public PlatformTransactionManager transactionManager() throws PropertyVetoException {
        return new DataSourceTransactionManager(dataSource());
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryBean")SqlSessionFactory sqlSessionFactoryBean ){
    	return new SqlSessionTemplate(sqlSessionFactoryBean);
    }

}
